const minio = require('../minio');
const router = require('../../router');
const url = require('url');

const {
    pagination,
    isEmpty,
    isArray
} = require('../../../utils');

router.get('/listBuckets', function (req, res) {

    const params = url.parse(req.url, true).query;

    minio(params.key).listBuckets(function (err, buckets) {
        if (err) return console.log(err)
        res.json({
            code: "0",
            // 分页处理
            data: isEmpty(params) ? 
                buckets : 
                isArray(buckets) ?
                 ( params.pageSize && params.pageNum ) ? 
                 pagination(buckets, params.pageSize, params.pageNum) : 
                 buckets : 
                 [],
            msg: '成功',
            success: true
        })
    })
})

module.exports = router;